package com.yuncheng.spcyApi.spcy;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.alog.utils.StringEmptyUtils;
import com.yuncheng.spcyApi.bo.JcyExistLbBo;
import com.yuncheng.spcyApi.bo.ParamsBo;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyCommonConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.constant.jcy.JcyPrdjConstant;
import com.yuncheng.spcyApi.constant.jcy.JcydjbConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.spcyApi.flow.XzswspFlowService;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.spcyService.ZdyUploadFileService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.StringPatternUtils;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import com.yuncheng.spcyApi.vo.jcy.JcyLxSddjVo;
import com.yuncheng.spcyApi.vo.jcy.RsHcyVo;
import com.yuncheng.vo.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import utils.DateConversionUtils;
import utils.ExcelUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author hua
 */
@Api(tags = "检查员聘任登记管理")
@RestController
@RequestMapping("/api/spzx-spcy/jcyprdj")
public class JcyPrController {

    @Resource
    private ISpcyJcyPrjbxxService spcyJcyPrjbxxService;

    @Resource
    private ISpcyJcyPrrymdService spcyJcyPrrymdService;

    @Resource
    private ISpcyRsHcyService spcyRsHcyService;

    @Resource
    private ISpcySqsxXpjlService spcySqsxXpjlService;

    @Resource
    private IYcSysUserService sysUserService;

    @Resource
    private ISpcyRsJcysfbgjlService spcyRsJcysfbgjlService;

    @Resource
    private ISpcyLogService spcyLogService;

    @Resource
    private ZdyUploadFileService zdyUploadFileService;

    @Resource
    private XzswspFlowService xzswspFlowService;

    @Log(title = "聘任登记-分页查询")
    @ApiOperation(value = "聘任登记-分页查询")
    @GetMapping(value = "prdjPageList")
    public HttpResult prdjPageList(SpcyJcyPrjbxx query, String gjz, PageVo pageVo, OrderItemVo orderItemVo){

        return HttpResult.ok(spcyJcyPrjbxxService.pageList(query, gjz, pageVo,orderItemVo));
    }

    @Log(title = "聘任登记-新增/保存")
    @ApiOperation(value = "聘任登记-新增/保存")
    @PostMapping(value = "savePrdj")
    public HttpResult savePrdj(@RequestBody SpcyJcyPrjbxx bean) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {

        String prsf = Optional.ofNullable(bean.getFprsf()).orElseGet(()->"");
        if (prsf.equals(SpcyConstant.HCY_SSK_ZY)){
            bean.setFsxjcyzg(SpcyConstant.HCY_SSK_YB);

        }else if (prsf.equals(SpcyConstant.HCY_SSK_ZZ)){
            bean.setFsxjcyzg(SpcyConstant.HCY_SSK_ZY);
        }

        if (StringEmptyUtils.isBlank(bean.getId())){

            bean = spcyJcyPrjbxxService.addPrdj(bean);
        }else {

            spcyJcyPrjbxxService.updatePrdj(bean);
        }

        return HttpResultVo.HttpResultSave(bean, "保存成功");
    }

    @Log(title = "聘任登记-删除")
    @ApiOperation(value = "聘任登记-删除")
    @DeleteMapping(value = "deletePrdj")
    public HttpResult deletePrdj(@RequestParam(value = "ids",defaultValue = "")String ids){
        if (org.apache.commons.lang3.StringUtils.isEmpty(ids)){
            throw new BusinessException("参数为空!");
        }

        List<String> listByIds = StringPatternUtils.getListByIds(ids);

        List<String> tyDelIdList = spcyJcyPrjbxxService.deletePrdj(listByIds);

        // 清空附件
        if (CollectionUtil.isNotEmpty(tyDelIdList)) {
            for (String id : tyDelIdList) {
                List<SpcySqsxWjjl> wjjlList = zdyUploadFileService.queryWjjlByPid(id);
                if (CollectionUtil.isNotEmpty(wjjlList)) {
                    for (SpcySqsxWjjl wjjl : wjjlList) {
                        zdyUploadFileService.deleteFileRecords(wjjl);
                        zdyUploadFileService.deleteFileByUrl(wjjl.getFcflj());
                    }
                }
            }
        }
        return HttpResult.ok("删除成功");
    }

    @Log(title = "聘任登记-附件上传")
    @ApiOperation(value = "聘任登记-附件上传")
    @PostMapping(value = "uploadPrdjFile")
    public HttpResult<?> uploadPrdjFile(@RequestParam("file") MultipartFile[] file,
                                        @RequestParam(value = "id",defaultValue = "")String id){
        if (StringEmptyUtils.isBlank(id)){
            throw new BusinessException("参数为空！");
        }
        SpcyJcyPrjbxx bean = spcyJcyPrjbxxService.getById(id);
        if (bean == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        List<SpcySqsxWjjl> wjjlList = zdyUploadFileService.queryWjjlByPid(id);
        if (CollectionUtil.isNotEmpty(wjjlList)){
            for (SpcySqsxWjjl wjjl : wjjlList) {
                zdyUploadFileService.deleteFileRecords(wjjl);
                zdyUploadFileService.deleteFileByUrl(wjjl.getFcflj());
            }
        }

        String wjid = GetUuIdUtils.ReplaceUuId();
        zdyUploadFileService.uploadFile(file, id, LogConstant.LOGS_JCY_PRDJGL, SpcyConstant.JCY_PRDJ_FILE_BASE_URL+"/"+ DateConversionUtils.DateToYear(new Date()), wjid);

        String wjHttpUrl = zdyUploadFileService.genWjllHttpUrl(wjid);
        bean.setFprfjdz(wjHttpUrl);
        spcyJcyPrjbxxService.updateById(bean);

        return HttpResultVo.HttpResultSave(bean,"上传成功");
    }

    @Log(title = "聘任登记-提交")
    @ApiOperation(value = "聘任登记-提交")
    @PostMapping(value = "submitPrdj")
    public HttpResult submitPrdj(@RequestBody SpcyJcyPrjbxx bean){
        if (StringEmptyUtils.isBlank(bean.getId())){
            throw new BusinessException("参数为空！");
        }
        SpcyJcyPrjbxx query = spcyJcyPrjbxxService.getById(bean.getId());
        if (query == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        if (StringEmptyUtils.isBlank(query.getFxpjcylb())){
            throw new BusinessException("请选择拟聘检查员类别！");
        }

        String przt = JcyPrdjConstant.getNextPrZt(query.getFprzt());

        /*if (przt.equals(JcyPrdjConstant.PR_JHZT_JHSH)){
            xzswspFlowService.verifyJcyPrInNpmdLcIsEnd(query.getId());

        }else if (przt.equals(JcyPrdjConstant.PR_JHZT_PRWC)){
            xzswspFlowService.verifyJcyPrInPrmdQrLcIsEnd(query.getId());
        }*/

        query.setFprzt(przt);
        spcyJcyPrjbxxService.updateById(query);

        if (przt.equals(JcyPrdjConstant.PR_JHZT_PRWC)){
            if (StringEmptyUtils.isBlank(query.getFprsf())){
                throw new BusinessException("请选择聘任身份！");
            }

            List<SpcyJcyPrrymd> rymdList = spcyJcyPrrymdService.list(new LambdaQueryWrapper<SpcyJcyPrrymd>()
                    .eq(SpcyJcyPrrymd::getFprjbxxid, bean.getId()));

            if (CollectionUtil.isNotEmpty(rymdList)){
                for (SpcyJcyPrrymd rymd : rymdList) {
                    if (StringUtils.isBlank(rymd.getFsfsdtj()) || rymd.getFsfsdtj().equals(JcyPrdjConstant.PR_RYMD_SFSDTJ_NO)) {
//                        submitBgPrryByNoSdtj(rymd, query);
                        submitBgPrryByNoSdtj2(rymd, query);
                    }else {
                        submitBgPrrySfByYesSdtj(rymd, query);
                    }

//                    sysUserService.updateUserGzztInZg(rymd.getFuserid());
                }
            }


            LambdaUpdateWrapper<SpcyJcyPrjbxx> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcyJcyPrjbxx::getId, bean.getId());

            SpcyJcyPrjbxx updatePrxx = new SpcyJcyPrjbxx();
            updatePrxx.setFprrs(rymdList.size());
            spcyJcyPrjbxxService.update(updatePrxx, updateWrapper);

        }

        String log = "从["+JcyPrdjConstant.getPreviousPrZt(przt)+"]提交到["+przt+"]";
        spcyLogService.addLogs(LogConstant.LOGS_JCY_PRDJGL,log,bean.getId(), "聘任登记-提交", SpcyConstant.LOGS_SYS_TYPE_TJ);

        return HttpResultVo.HttpResultSave(query, "提交成功");
    }

    @Log(title = "聘任登记-退回")
    @ApiOperation(value = "聘任登记-退回")
    @PostMapping(value = "returnPrdj")
    public HttpResult returnPrdj(@RequestBody SpcyJcyPrjbxx bean){
        if (org.apache.commons.lang3.StringUtils.isBlank(bean.getId())){
            throw new BusinessException("参数为空！");
        }

        SpcyJcyPrjbxx query = spcyJcyPrjbxxService.getById(bean.getId());
        if (query == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        if (org.apache.commons.lang3.StringUtils.isBlank(query.getFprzt())){
            throw new BusinessException("退回失败，参数为空！");
        }

        /*if (query.getFprzt().equals(JcyPrdjConstant.PR_JHZT_PRWC)){
            throw new BusinessException("退回失败，该聘任已经["+JcyPrdjConstant.PR_JHZT_PRWC+"]!!");
        }*/

        String preZt = JcyPrdjConstant.getPreviousPrZt(query.getFprzt());
        query.setFprzt(preZt);
        spcyJcyPrjbxxService.updateById(query);

        String log = "从["+JcyPrdjConstant.getPreviousPrZt(preZt)+"]退回到["+preZt+"]";
        spcyLogService.addLogs(LogConstant.LOGS_JCY_PRDJGL,log,bean.getId(), "聘任登记-退回", SpcyConstant.LOGS_SYS_TYPE_TH);

        // 完成退回到登记
        if (preZt.equals(JcyPrdjConstant.PR_JHZT_PRDJ)) {
            List<SpcyJcyPrrymd> rymdList = spcyJcyPrrymdService.list(new LambdaQueryWrapper<SpcyJcyPrrymd>()
                    .eq(SpcyJcyPrrymd::getFprjbxxid, bean.getId()));

            if (CollectionUtil.isNotEmpty(rymdList)) {
                for (SpcyJcyPrrymd rymd : rymdList) {

                    if (StringUtils.isBlank(rymd.getFsfsdtj()) || rymd.getFsfsdtj().equals(JcyPrdjConstant.PR_RYMD_SFSDTJ_NO)) {
//                        returnBgPrryByNoSdtj(rymd, query);
                        returnBgPrryByNoSdtj2(rymd, query);
                    }else {
                        returnBgPrryByYesSdtj(rymd, query);
                    }
                }
            }
        }


        return HttpResult.ok("退回成功");
    }

    @Log(title = "聘任登记-根据id查看")
    @ApiOperation(value = "聘任登记-根据id查看")
    @GetMapping(value = "queryPrdjById")
    public HttpResult queryPrdjById(@RequestParam(value = "id",defaultValue = "")String id){
        if (StringEmptyUtils.isBlank(id)){
            throw new BusinessException("参数为空！");
        }
        SpcyJcyPrjbxx jbxx = spcyJcyPrjbxxService.getById(id);
        return HttpResult.ok(jbxx);
    }

    @Log(title = "获取手动添加人员-分页查询")
    @ApiOperation(value = "获取手动添加人员-分页查询")
    @GetMapping(value = "getSdtjryPageList")
    public HttpResult getSdtjryPageList(YcSysUser query,String gjz, PageVo pageVo){
        return HttpResult.ok(sysUserService.getPrSdtjryPageList(query, gjz, pageVo));
    }

    @Log(title = "拟聘人员名单-分页查询")
    @ApiOperation(value = "拟聘人员名单-分页查询")
    @GetMapping(value = "nprymdPageList")
    public HttpResult nprymdPageList(SpcyJcyPrrymd query, String gjz, PageVo pageVo, OrderItemVo orderItemVo){
        if (StringEmptyUtils.isBlank(query.getId())){
            query.setId("0");
        }
        return HttpResult.ok(spcyJcyPrrymdService.pageList(query, gjz, pageVo,orderItemVo));
    }

    @Log(title = "拟聘人员名单-移除名单")
    @ApiOperation(value = "拟聘人员名单-移除名单")
    @PostMapping(value = "nprymdByYcmd")
    public HttpResult nprymdByYcmd(@RequestBody ParamsBo paramsBo){
        paramsBo.getIds();
        if (StringEmptyUtils.isBlank(paramsBo.getIds())){
            throw new BusinessException("请选择参数！");
        }
        List<String> idsList = StringPatternUtils.getListByIds(paramsBo.getIds());
        spcyJcyPrrymdService.removeBatchByIds(idsList);

        return HttpResult.ok("移除成功");
    }

    @Log(title = "拟聘人员名单-导出名单")
    @ApiOperation(value = "拟聘人员名单-导出名单")
    @GetMapping(value = "exportNprymd")
    public void exportNprymd(@RequestParam(value = "id",defaultValue = "")String id, HttpServletResponse response){

        if (StringEmptyUtils.isBlank(id)){
            throw new BusinessException("聘任参数为空！");
        }

        String[] titles = new String[]{"序号", "姓名", "性别","工作单位", "检查类型", "检查次数"};
        List<List<String>> resultList = new ArrayList<>();

        resultList.add(CollectionUtil.newArrayList(titles));

        List<String> dataList = null;

        LambdaQueryWrapper<SpcyJcyPrrymd> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcyJcyPrrymd::getFprjbxxid, id);
        queryWrapper.orderByDesc(SpcyJcyPrrymd::getCreateTime);

        List<SpcyJcyPrrymd> userList = spcyJcyPrrymdService.list(queryWrapper);
        for (int order = 0; order < userList.size(); order++){
            dataList = new ArrayList<>();

            dataList.add(String.valueOf(order+1));

            String sex = "";
            if (userList.get(order).getSex() != null){
                if (userList.get(order).getSex().equals(1)){
                    sex = "男";
                }else if (userList.get(order).getSex().equals(2)){
                    sex = "女";
                }
            }

            dataList.add(userList.get(order).getRealname());
            dataList.add(sex);

//            dataList.add(userList.get(order).getFsfzh());
            dataList.add(userList.get(order).getFgzdw());

            dataList.add(userList.get(order).getFjcyxl());
            dataList.add(userList.get(order).getFjccs());


            resultList.add(dataList);
        }

        // 文件名称
        String title = "拟聘任人员名单";
        ExcelUtils.getLocalTitleExcel(resultList,title,response);
    }

    @Log(title = "聘任登记-生成拟聘人员名单")
    @ApiOperation(value = "聘任登记-生成拟聘人员名单")
    @PostMapping(value = "genNprymd")
    public HttpResult genNprymd(@RequestBody SpcyJcyPrjbxx query) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (StringEmptyUtils.isBlank(query.getId())){
            throw new BusinessException("聘任参数为空!");
        }
        /*if (StringEmptyUtils.isBlank(query.getFxpjcylb())){
            throw new BusinessException("请选择选聘的检查员类别！");
        }
        if (StringEmptyUtils.isBlank(query.getFprsf())){
            throw new BusinessException("请选择聘任身份！");
        }*/

        query.setFsfjmzyq(Optional.ofNullable(query.getFsfjmzyq()).orElseGet(()->"否"));
        String sfjmzyq = query.getFsfjmzyq();  // 是否要求所选检查类别均满足上述要求

        String xpjcylb = Optional.ofNullable(query.getFxpjcylb()).orElseGet(()->""); // 选派检查员类别

        query.setFyqjccs(Optional.ofNullable(query.getFyqjccs()).orElseGet(()->0));
        int yqjccs = query.getFyqjccs(); // 符合检查次数

        String yqkpjg = Optional.ofNullable(query.getFyqkpjg()).orElseGet(()->""); // 考评结果

        String sxkssj = Optional.ofNullable(query.getFsxkssj()).orElseGet(()->""); // 筛选开始时间
        String sxjssj = Optional.ofNullable(query.getFsxjssj()).orElseGet(()->""); // 筛选结束时间

        String prsf = Optional.ofNullable(query.getFprsf()).orElseGet(()->"");

        /*if (prsf.equals(SpcyConstant.HCY_SSK_ZY)){
            query.setFsxjcyzg(SpcyConstant.HCY_SSK_YB);

        }else if (prsf.equals(SpcyConstant.HCY_SSK_ZZ)){
            query.setFsxjcyzg(SpcyConstant.HCY_SSK_ZY);
        }*/

        spcyJcyPrjbxxService.updatePrdj(query);

        // 清空上一次的拟聘任人员名单(不包含手动登记)
        spcyJcyPrrymdService.remove(new LambdaQueryWrapper<SpcyJcyPrrymd>()
                .eq(SpcyJcyPrrymd::getFprjbxxid, query.getId())
                .ne(SpcyJcyPrrymd::getFsfsdtj, JcyPrdjConstant.PR_RYMD_SFSDTJ_YES));

        // 拟聘任检查员名单
        List<SpcyJcyPrrymd> newRymdList = new ArrayList<>();

        if (sfjmzyq.equals("是")) {

//            newRymdList = genNprymdByYes(newRymdList, query.getId(), query.getFsxjcyzg(), xpjcylb, yqkpjg, sxkssj, sxjssj, yqjccs);
//            newRymdList = genNprymdByYes(newRymdList, query.getId(), SpcyConstant.HCY_SSK_YB, xpjcylb, yqkpjg, sxkssj, sxjssj, yqjccs);
            newRymdList = genNprymdByYes2(newRymdList, query.getId(), SpcyConstant.HCY_SSK_YB, xpjcylb, yqkpjg, sxkssj, sxjssj, yqjccs);

        }else if (sfjmzyq.equals("否")){

//            newRymdList = genNprymdByNo(newRymdList, query.getId(), query.getFsxjcyzg(), xpjcylb, yqkpjg, sxkssj, sxjssj, yqjccs);
//            newRymdList = genNprymdByNo(newRymdList, query.getId(), SpcyConstant.HCY_SSK_YB, xpjcylb, yqkpjg, sxkssj, sxjssj, yqjccs);
            newRymdList = genNprymdByNo2(newRymdList, query.getId(), SpcyConstant.HCY_SSK_YB, xpjcylb, yqkpjg, sxkssj, sxjssj, yqjccs);
        }

        if (CollectionUtil.isNotEmpty(newRymdList)){
            spcyJcyPrrymdService.saveBatch(newRymdList);
        }
        return HttpResult.ok("生成成功");
    }

    @Log(title = "聘任登记-手动添加人员")
    @ApiOperation(value = "聘任登记-手动添加人员")
    @PostMapping(value = "sddjRymd")
    public HttpResult sddjRymd(@RequestBody JcyLxSddjVo sddjVo){
        if (StringUtils.isBlank(sddjVo.getFprjbxxid())){
            throw new BusinessException("参数为空！");
        }

        SpcyJcyPrjbxx prdj = spcyJcyPrjbxxService.getById(sddjVo.getFprjbxxid());
        if (prdj == null){
            throw new BusinessException("没有相匹配的数据！");
        }

        if (StringUtils.isBlank(sddjVo.getIds())){
            throw new BusinessException("请选择人员！");
        }
        if (StringUtils.isBlank(prdj.getFxpjcylb())){
            throw new BusinessException("请选择检查员类别并保存！");
        }
        List<SpcyJcyPrrymd> newAddPrrymdList = new ArrayList<>();
        List<String> idsList = StringPatternUtils.getListByIds(sddjVo.getIds());
        List<YcSysUser> userList = sysUserService.listByIds(idsList);
        for (int i=0; i<userList.size(); i++){
            YcSysUser user = userList.get(i);

            SpcyJcyPrrymd mdBean = sddjRymdService(prdj, user, sddjVo.getFprjbxxid());
            if (mdBean != null){
                newAddPrrymdList.add(mdBean);
            }
        }

        if (CollectionUtil.isNotEmpty(newAddPrrymdList)){
            spcyJcyPrrymdService.saveBatch(newAddPrrymdList);
        }

        return HttpResult.ok("添加成功");
    }

    /**
     * 是否要求所选检查类别均满足上述要求: 是
     * */
    private List<SpcyJcyPrrymd> genNprymdByYes(List<SpcyJcyPrrymd> newRymdList, String prjbxxid, String ssk, String xpjcylb, String yqkpjg, String sxkssj, String sxjssj, int yqjccs){

        RsHcyVo rsHcyVo = new RsHcyVo();
        rsHcyVo.setFusertype(SpcyConstant.HCY_USERTYPE_JCY);
        rsHcyVo.setFstate(SpcyCommonConstant.JCY_STATE_YX);
        rsHcyVo.setFssk(ssk);
//        rsHcyVo.setFjcyxl(xpjcylb);
        rsHcyVo.setFlx(xpjcylb);
        List<RsHcyVo> hcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

        if (CollectionUtil.isEmpty(hcyList)){
            return newRymdList;
        }

        for (RsHcyVo jcy : hcyList) {
            LambdaQueryWrapper<SpcySqsxXpjl> xpQueryWrapper = new LambdaQueryWrapper<>();
            xpQueryWrapper.eq(SpcySqsxXpjl::getFjcyid, jcy.getId());
            xpQueryWrapper.eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_YES);
            if (StringEmptyUtils.isNotBlank(yqkpjg)){
                xpQueryWrapper.in(SpcySqsxXpjl::getFzhpj, StringPatternUtils.getListByIds(yqkpjg));
            }
            if (StringEmptyUtils.isNotBlank(sxkssj)){
                xpQueryWrapper.ge(SpcySqsxXpjl::getFgzkssj, DateConversionUtils.convertStringToDateYmd(sxkssj));
            }
            if (StringEmptyUtils.isNotBlank(sxjssj)){
                xpQueryWrapper.le(SpcySqsxXpjl::getFgzjssj, DateConversionUtils.convertStringToDateYmd(sxjssj));
            }
            List<SpcySqsxXpjl> xpjlList = spcySqsxXpjlService.list(xpQueryWrapper);

            if (xpjlList.size() >= yqjccs){

                SpcyJcyPrrymd newRymd = new SpcyJcyPrrymd();
                newRymd.setFprjbxxid(prjbxxid);
                newRymd.setId(GetUuIdUtils.ReplaceUuId());

                newRymd.setFgzdw(jcy.getFgzdw());
                newRymd.setRealname(jcy.getRealname());
                newRymd.setSex(jcy.getSex());

                newRymd.setFsfzh(jcy.getFsfzh());
                newRymd.setFjcyid(jcy.getId());
                newRymd.setFuserid(jcy.getFuserid());

                newRymd.setFsxzy(jcy.getFsxzy());
                newRymd.setFxl(jcy.getFxl());

                newRymd.setFjcyxl(jcy.getFjcyxl());

                String jccs = "";
                if (xpjlList.size() > 0){
                    if (StringEmptyUtils.isNotBlank(yqkpjg)) {
                        List<String> yqkpkgList = StringPatternUtils.getListByIds(yqkpjg);
                        for (String kpjg : yqkpkgList) {
                            int kpjgSize = xpjlList.stream()
                                    .filter(f -> StringEmptyUtils.isNotBlank(f.getFzhpj())
                                            && f.getFzhpj().equals(kpjg))
                                    .collect(Collectors.toList()).size();

                            if (StringEmptyUtils.isBlank(jccs)){
                                jccs += kpjg + " " + kpjgSize;
                            }else {
                                jccs += "," + kpjg + " " + kpjgSize;
                            }
                        }
                    }else {
                        jccs = String.valueOf(xpjlList.size());
                    }
                }else {
                    jccs = "0";
                }
                newRymd.setFjccs(jccs);

                newRymd.setFsfsdtj(JcyPrdjConstant.PR_RYMD_SFSDTJ_NO);
                newRymd.setCreateTime(new Date());
                newRymd.setUpdateTime(new Date());
                newRymd.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
                newRymd.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
                newRymd.setSysOrgCode(GetCurrentUserUtils.getCurrentUserOrgCode());
                newRymdList.add(newRymd);
            }
        }

        return newRymdList;
    }

    /**
     * 是否要求所选检查类别均满足上述要求: 是 （版本2）
     * */
    private List<SpcyJcyPrrymd> genNprymdByYes2(List<SpcyJcyPrrymd> newRymdList, String prjbxxid, String ssk, String xpjcylb, String yqkpjg, String sxkssj, String sxjssj, int yqjccs){

        RsHcyVo rsHcyVo = new RsHcyVo();
        rsHcyVo.setFusertype(SpcyConstant.HCY_USERTYPE_JCY);
        rsHcyVo.setFstate(SpcyCommonConstant.JCY_STATE_YX);
        rsHcyVo.setFssk(ssk);
//        rsHcyVo.setFjcyxl(xpjcylb);
        rsHcyVo.setFlx(xpjcylb);
        List<RsHcyVo> hcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

        if (CollectionUtil.isEmpty(hcyList)){
            return newRymdList;
        }

        List<String> userIdList = new ArrayList<>();
        for (RsHcyVo jcy : hcyList) {

            // 存在不处理
            if (userIdList.contains(jcy.getFuserid())){
                continue;
            }

            userIdList.add(jcy.getFuserid());

            boolean isAddMdFlag = false;

            LambdaQueryWrapper<SpcySqsxXpjl> xpQueryWrapper = new LambdaQueryWrapper<>();
            xpQueryWrapper.eq(SpcySqsxXpjl::getFuserid, jcy.getFuserid());
            xpQueryWrapper.eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_YES);
            xpQueryWrapper.in(SpcySqsxXpjl::getFlx, StringPatternUtils.getListByIds(xpjcylb));
            if (StringEmptyUtils.isNotBlank(yqkpjg)){
                xpQueryWrapper.in(SpcySqsxXpjl::getFzhpj, StringPatternUtils.getListByIds(yqkpjg));
            }
            if (StringEmptyUtils.isNotBlank(sxkssj)){
                xpQueryWrapper.ge(SpcySqsxXpjl::getFgzkssj, DateConversionUtils.convertStringToDateYmd(sxkssj));
            }
            if (StringEmptyUtils.isNotBlank(sxjssj)){
                xpQueryWrapper.le(SpcySqsxXpjl::getFgzjssj, DateConversionUtils.convertStringToDateYmd(sxjssj));
            }
            List<SpcySqsxXpjl> xpjlList = spcySqsxXpjlService.list(xpQueryWrapper);

            int ywlcSize = 0;
            String ywlcBz = "";

            int ypzcscSize = 0;
            String ypzcscBz = "";

            int ypltSize = 0;
            String ypltBz = "";

            int ylqxscltSize = 0;
            String ylqxscltBz = "";

            int ylqxlcSize = 0;
            String ylqxlcBz = "";

            int hzpSize = 0;
            String hzpBz = "";

            String bz = "";
            String jccs = "";

            if (CollectionUtil.isNotEmpty(xpjlList)){
                for (SpcySqsxXpjl xpjl : xpjlList){
                    if (StringUtils.isNotBlank(xpjl.getFlx())){
                        if (xpjl.getFlx().equals(JcydjbConstant.LB_YPLC)){
                            ywlcSize += 1;

                            if (StringUtils.isNotBlank(ywlcBz)){
                                ywlcBz += "\n";
                            }
                            ywlcBz += ywlcSize + "、";
                            ywlcBz += Optional.ofNullable(xpjl.getFrwlx()).orElseGet(()->"无");
                            ywlcBz += " ";
                            ywlcBz += xpjl.getFgzkssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzkssj()) : "无";
                            ywlcBz += " ";
                            ywlcBz += xpjl.getFgzjssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzjssj()) : "无";
                            ywlcBz += " ";
                            ywlcBz += Optional.ofNullable(xpjl.getFzhpj()).orElseGet(()->"无");

                        }else if (xpjl.getFlx().equals(JcydjbConstant.LB_YPZCSC)){
                            ypzcscSize += 1;

                            if (StringUtils.isNotBlank(ypzcscBz)){
                                ypzcscBz += "\n";
                            }
                            ypzcscBz += ypzcscSize + "、";
                            ypzcscBz += Optional.ofNullable(xpjl.getFrwlx()).orElseGet(()->"无");
                            ypzcscBz += " ";
                            ypzcscBz += xpjl.getFgzkssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzkssj()) : "无";
                            ypzcscBz += " ";
                            ypzcscBz += xpjl.getFgzjssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzjssj()) : "无";
                            ypzcscBz += " ";
                            ypzcscBz += Optional.ofNullable(xpjl.getFzhpj()).orElseGet(()->"无");

                        }else if (xpjl.getFlx().equals(JcydjbConstant.LB_YPLT)){
                            ypltSize += 1;

                            if (StringUtils.isNotBlank(ypltBz)){
                                ypltBz += "\n";
                            }
                            ypltBz += ypltSize + "、";
                            ypltBz += Optional.ofNullable(xpjl.getFrwlx()).orElseGet(()->"无");
                            ypltBz += " ";
                            ypltBz += xpjl.getFgzkssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzkssj()) : "无";
                            ypltBz += " ";
                            ypltBz += xpjl.getFgzjssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzjssj()) : "无";
                            ypltBz += " ";
                            ypltBz += Optional.ofNullable(xpjl.getFzhpj()).orElseGet(()->"无");

                        }else if (xpjl.getFlx().equals(JcydjbConstant.LB_YLQXSCLT)){
                            ylqxscltSize += 1;

                            if (StringUtils.isNotBlank(ylqxscltBz)){
                                ylqxscltBz += "\n";
                            }
                            ylqxscltBz += ylqxscltSize + "、";
                            ylqxscltBz += Optional.ofNullable(xpjl.getFrwlx()).orElseGet(()->"无");
                            ylqxscltBz += " ";
                            ylqxscltBz += xpjl.getFgzkssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzkssj()) : "无";
                            ylqxscltBz += " ";
                            ylqxscltBz += xpjl.getFgzjssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzjssj()) : "无";
                            ylqxscltBz += " ";
                            ylqxscltBz += Optional.ofNullable(xpjl.getFzhpj()).orElseGet(()->"无");

                        }else if (xpjl.getFlx().equals(JcydjbConstant.LB_YLQXLC)){
                            ylqxlcSize += 1;

                            if (StringUtils.isNotBlank(ylqxlcBz)){
                                ylqxlcBz += "\n";
                            }
                            ylqxlcBz += ylqxlcSize + "、";
                            ylqxlcBz += Optional.ofNullable(xpjl.getFrwlx()).orElseGet(()->"无");
                            ylqxlcBz += " ";
                            ylqxlcBz += xpjl.getFgzkssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzkssj()) : "无";
                            ylqxlcBz += " ";
                            ylqxlcBz += xpjl.getFgzjssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzjssj()) : "无";
                            ylqxlcBz += " ";
                            ylqxlcBz += Optional.ofNullable(xpjl.getFzhpj()).orElseGet(()->"无");

                        }else if (xpjl.getFlx().equals(JcydjbConstant.LB_HZPJCY)){
                            hzpSize += 1;

                            if (StringUtils.isNotBlank(hzpBz)){
                                hzpBz += "\n";
                            }
                            hzpBz += hzpSize + "、";
                            hzpBz += Optional.ofNullable(xpjl.getFrwlx()).orElseGet(()->"无");
                            hzpBz += " ";
                            hzpBz += xpjl.getFgzkssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzkssj()) : "无";
                            hzpBz += " ";
                            hzpBz += xpjl.getFgzjssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzjssj()) : "无";
                            hzpBz += " ";
                            hzpBz += Optional.ofNullable(xpjl.getFzhpj()).orElseGet(()->"无");

                        }
                    }
                }
            }

            if (StringUtils.isNotBlank(ywlcBz)){
                bz += JcydjbConstant.LB_YPLC + ": " + "\n" + ywlcBz;
            }

            if (StringUtils.isNotBlank(ypzcscBz)){
                if (StringUtils.isNotBlank(bz)){
                    bz += "\n";
                }

                bz += JcydjbConstant.LB_YPZCSC + ": " + "\n" + ypzcscBz;
            }

            if (StringUtils.isNotBlank(ypltBz)){
                if (StringUtils.isNotBlank(bz)){
                    bz += "\n";
                }

                bz += JcydjbConstant.LB_YPLT + ": " + "\n" + ypltBz;
            }

            if (StringUtils.isNotBlank(ylqxscltBz)){
                if (StringUtils.isNotBlank(bz)){
                    bz += "\n";
                }

                bz += JcydjbConstant.LB_YLQXSCLT + ": " + "\n" + ylqxscltBz;
            }

            if (StringUtils.isNotBlank(ylqxlcBz)){
                if (StringUtils.isNotBlank(bz)){
                    bz += "\n";
                }

                bz += JcydjbConstant.LB_YLQXLC + ": " + "\n" + ylqxlcBz;
            }

            if (StringUtils.isNotBlank(hzpBz)){
                if (StringUtils.isNotBlank(bz)){
                    bz += "\n";
                }

                bz += JcydjbConstant.LB_HZPJCY + ": " + "\n" + hzpBz;
            }

            if (ywlcSize > 0){
                jccs += JcydjbConstant.LB_YPLC + "("+ywlcSize+")";
            }

            if (ypzcscSize > 0){
                if (StringUtils.isNotBlank(jccs)){
                    jccs += "/";
                }
                jccs += JcydjbConstant.LB_YPZCSC + "("+ypzcscSize+")";
            }


            if (ypltSize > 0){
                if (StringUtils.isNotBlank(jccs)){
                    jccs += "/";
                }
                jccs += JcydjbConstant.LB_YPLT + "("+ypltSize+")";
            }

            if (ylqxscltSize > 0){
                if (StringUtils.isNotBlank(jccs)){
                    jccs += "/";
                }
                jccs += JcydjbConstant.LB_YLQXSCLT + "("+ylqxscltSize+")";
            }

            if (ylqxlcSize > 0){
                if (StringUtils.isNotBlank(jccs)){
                    jccs += "/";
                }
                jccs += JcydjbConstant.LB_YLQXLC + "("+ylqxlcSize+")";
            }

            if (hzpSize > 0){
                if (StringUtils.isNotBlank(jccs)){
                    jccs += "/";
                }
                jccs += JcydjbConstant.LB_HZPJCY + "("+hzpSize+")";
            }

            if (xpjcylb.contains(JcydjbConstant.LB_YPLC)){
                isAddMdFlag = (ywlcSize > yqjccs) ? true : false;
            }
            if (xpjcylb.contains(JcydjbConstant.LB_YPZCSC)){
                isAddMdFlag = (ypzcscSize > yqjccs) ? true : false;
            }
            if (xpjcylb.contains(JcydjbConstant.LB_YPLT)){
                isAddMdFlag = (ypltSize > yqjccs) ? true : false;
            }
            if (xpjcylb.contains(JcydjbConstant.LB_YLQXSCLT)){
                isAddMdFlag = (ylqxscltSize > yqjccs) ? true : false;
            }
            if (xpjcylb.contains(JcydjbConstant.LB_YLQXLC)){
                isAddMdFlag = (ylqxlcSize > yqjccs) ? true : false;
            }
            if (xpjcylb.contains(JcydjbConstant.LB_HZPJCY)){
                isAddMdFlag = (hzpSize > yqjccs) ? true : false;
            }

            if (isAddMdFlag) {
                SpcyJcyPrrymd newRymd = new SpcyJcyPrrymd();
                newRymd.setFprjbxxid(prjbxxid);
                newRymd.setId(GetUuIdUtils.ReplaceUuId());
                newRymd.setFjcyid(jcy.getId());
                newRymd.setFuserid(jcy.getFuserid());

                newRymd.setFgzdw(jcy.getFgzdw());
                newRymd.setRealname(jcy.getRealname());
                newRymd.setSex(jcy.getSex());

                newRymd.setFsfzh(jcy.getFsfzh());
                newRymd.setFsxzy(jcy.getFsxzy());
                newRymd.setFxl(jcy.getFxl());

                newRymd.setFjcyxl(xpjcylb);

                newRymd.setFjccs(jccs);
                newRymd.setFbz(bz);

                newRymd.setFsfsdtj(JcyPrdjConstant.PR_RYMD_SFSDTJ_NO);
                newRymd.setCreateTime(new Date());
                newRymd.setUpdateTime(new Date());
                newRymd.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
                newRymd.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
                newRymd.setSysOrgCode(GetCurrentUserUtils.getCurrentUserOrgCode());
                newRymdList.add(newRymd);
            }
        }

        return newRymdList;
    }

    /**
     * 是否要求所选检查类别均满足上述要求: 否
     * */
    private List<SpcyJcyPrrymd> genNprymdByNo(List<SpcyJcyPrrymd> newRymdList, String prjbxxid, String ssk, String xpjcylb, String yqkpjg, String sxkssj, String sxjssj, int yqjccs){
        RsHcyVo rsHcyVo = new RsHcyVo();
        rsHcyVo.setFusertype(SpcyConstant.HCY_USERTYPE_JCY);
        rsHcyVo.setFstate(SpcyCommonConstant.JCY_STATE_YX);
        rsHcyVo.setFssk(ssk);
//        rsHcyVo.setFjcyxl(xpjcylb);
        rsHcyVo.setFlx(xpjcylb);
        List<RsHcyVo> hcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

        if (CollectionUtil.isEmpty(hcyList)){
            return newRymdList;
        }

        for (RsHcyVo jcy : hcyList) {
            LambdaQueryWrapper<SpcySqsxXpjl> xpQueryWrapper = new LambdaQueryWrapper<>();
            xpQueryWrapper.eq(SpcySqsxXpjl::getFuserid, jcy.getFuserid());
            xpQueryWrapper.eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_YES);
//            xpQueryWrapper.in(SpcySqsxXpjl::getFjcyxl, StringPatternUtils.getListByIds(xpjcylb));
            xpQueryWrapper.in(SpcySqsxXpjl::getFlx, StringPatternUtils.getListByIds(xpjcylb));
            if (StringEmptyUtils.isNotBlank(yqkpjg)){
                xpQueryWrapper.in(SpcySqsxXpjl::getFzhpj, StringPatternUtils.getListByIds(yqkpjg));
            }
            if (StringEmptyUtils.isNotBlank(sxkssj)){
                xpQueryWrapper.ge(SpcySqsxXpjl::getFgzkssj, DateConversionUtils.convertStringToDateYmd(sxkssj));
            }
            if (StringEmptyUtils.isNotBlank(sxjssj)){
                xpQueryWrapper.le(SpcySqsxXpjl::getFgzjssj, DateConversionUtils.convertStringToDateYmd(sxjssj));
            }
            List<SpcySqsxXpjl> xpjlList = spcySqsxXpjlService.list(xpQueryWrapper);

            if (xpjlList.size() >= yqjccs){
                SpcyJcyPrrymd newRymd = new SpcyJcyPrrymd();
                newRymd.setFprjbxxid(prjbxxid);
                newRymd.setId(GetUuIdUtils.ReplaceUuId());
                newRymd.setFuserid(jcy.getFuserid());
                newRymd.setFjcyid(jcy.getId());

                newRymd.setFgzdw(jcy.getFgzdw());
                newRymd.setRealname(jcy.getRealname());
                newRymd.setSex(jcy.getSex());

                newRymd.setFsfzh(jcy.getFsfzh());
                newRymd.setFsxzy(jcy.getFsxzy());
                newRymd.setFxl(jcy.getFxl());

                newRymd.setFjcyxl(jcy.getFjcyxl());

                String jccs = "";
                if (xpjlList.size() > 0){
                    List<SpcySqsxXpjl> jcyXpList = xpjlList.stream()
                            .filter(f -> StringEmptyUtils.isNotBlank(f.getFjcyid()) && f.getFjcyid().equals(jcy.getId()))
                            .collect(Collectors.toList());

                    if (CollectionUtil.isNotEmpty(jcyXpList)){
                        if (StringEmptyUtils.isNotBlank(yqkpjg)) {
                            List<String> yqkpkgList = StringPatternUtils.getListByIds(yqkpjg);
                            for (String kpjg : yqkpkgList) {
                                int kpjgSize = jcyXpList.stream()
                                        .filter(f -> StringEmptyUtils.isNotBlank(f.getFzhpj())
                                                && f.getFzhpj().equals(kpjg))
                                        .collect(Collectors.toList()).size();

                                if (StringEmptyUtils.isBlank(jccs)){
                                    jccs += kpjg + " " + kpjgSize;
                                }else {
                                    jccs += "," + kpjg + " " + kpjgSize;
                                }
                            }
                        }else {
                            jccs = String.valueOf(jcyXpList.size());
                        }
                    }else {
                        jccs = "0";
                    }
                }else {
                    jccs = "0";
                }
                newRymd.setFjccs(jccs);

                newRymd.setFsfsdtj(JcyPrdjConstant.PR_RYMD_SFSDTJ_NO);
                newRymd.setCreateTime(new Date());
                newRymd.setUpdateTime(new Date());
                newRymd.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
                newRymd.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
                newRymd.setSysOrgCode(GetCurrentUserUtils.getCurrentUserOrgCode());
                newRymdList.add(newRymd);
            }
        }

        return newRymdList;
    }

    /**
     * 是否要求所选检查类别均满足上述要求: 否 （版本2）
     * */
    private List<SpcyJcyPrrymd> genNprymdByNo2(List<SpcyJcyPrrymd> newRymdList, String prjbxxid, String ssk, String xpjcylb, String yqkpjg, String sxkssj, String sxjssj, int yqjccs){

        RsHcyVo rsHcyVo = new RsHcyVo();
        rsHcyVo.setFusertype(SpcyConstant.HCY_USERTYPE_JCY);
        rsHcyVo.setFstate(SpcyCommonConstant.JCY_STATE_YX);
        rsHcyVo.setFssk(ssk);
//        rsHcyVo.setFjcyxl(xpjcylb);
        rsHcyVo.setFlx(xpjcylb);
        List<RsHcyVo> hcyList = spcyRsHcyService.getRsHcyList(rsHcyVo);

        if (CollectionUtil.isEmpty(hcyList)){
            return newRymdList;
        }

        List<String> userIdList = new ArrayList<>();
        for (RsHcyVo jcy : hcyList) {

            // 存在不处理
            if (userIdList.contains(jcy.getFuserid())){
                continue;
            }

            userIdList.add(jcy.getFuserid());

            boolean isAddMdFlag = false;

            LambdaQueryWrapper<SpcySqsxXpjl> xpQueryWrapper = new LambdaQueryWrapper<>();
            xpQueryWrapper.eq(SpcySqsxXpjl::getFuserid, jcy.getFuserid());
            xpQueryWrapper.eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_YES);
            xpQueryWrapper.in(SpcySqsxXpjl::getFlx, StringPatternUtils.getListByIds(xpjcylb));
            if (StringEmptyUtils.isNotBlank(yqkpjg)){
                xpQueryWrapper.in(SpcySqsxXpjl::getFzhpj, StringPatternUtils.getListByIds(yqkpjg));
            }
            if (StringEmptyUtils.isNotBlank(sxkssj)){
                xpQueryWrapper.ge(SpcySqsxXpjl::getFgzkssj, DateConversionUtils.convertStringToDateYmd(sxkssj));
            }
            if (StringEmptyUtils.isNotBlank(sxjssj)){
                xpQueryWrapper.le(SpcySqsxXpjl::getFgzjssj, DateConversionUtils.convertStringToDateYmd(sxjssj));
            }
            List<SpcySqsxXpjl> xpjlList = spcySqsxXpjlService.list(xpQueryWrapper);

            int ywlcSize = 0;
            String ywlcBz = "";

            int ypzcscSize = 0;
            String ypzcscBz = "";

            int ypltSize = 0;
            String ypltBz = "";

            int ylqxscltSize = 0;
            String ylqxscltBz = "";

            int ylqxlcSize = 0;
            String ylqxlcBz = "";

            int hzpSize = 0;
            String hzpBz = "";

            String bz = "";
            String jccs = "";

            if (CollectionUtil.isNotEmpty(xpjlList)){
                for (SpcySqsxXpjl xpjl : xpjlList){
                    if (StringUtils.isNotBlank(xpjl.getFlx())){
                        if (xpjl.getFlx().equals(JcydjbConstant.LB_YPLC)){
                            ywlcSize += 1;

                            if (StringUtils.isNotBlank(ywlcBz)){
                                ywlcBz += "\n";
                            }
                            ywlcBz += ywlcSize + "、";
                            ywlcBz += Optional.ofNullable(xpjl.getFrwlx()).orElseGet(()->"无");
                            ywlcBz += " ";
                            ywlcBz += xpjl.getFgzkssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzkssj()) : "无";
                            ywlcBz += " ";
                            ywlcBz += xpjl.getFgzjssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzjssj()) : "无";
                            ywlcBz += " ";
                            ywlcBz += Optional.ofNullable(xpjl.getFzhpj()).orElseGet(()->"无");

                        }else if (xpjl.getFlx().equals(JcydjbConstant.LB_YPZCSC)){
                            ypzcscSize += 1;

                            if (StringUtils.isNotBlank(ypzcscBz)){
                                ypzcscBz += "\n";
                            }
                            ypzcscBz += ypzcscSize + "、";
                            ypzcscBz += Optional.ofNullable(xpjl.getFrwlx()).orElseGet(()->"无");
                            ypzcscBz += " ";
                            ypzcscBz += xpjl.getFgzkssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzkssj()) : "无";
                            ypzcscBz += " ";
                            ypzcscBz += xpjl.getFgzjssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzjssj()) : "无";
                            ypzcscBz += " ";
                            ypzcscBz += Optional.ofNullable(xpjl.getFzhpj()).orElseGet(()->"无");

                        }else if (xpjl.getFlx().equals(JcydjbConstant.LB_YPLT)){
                            ypltSize += 1;

                            if (StringUtils.isNotBlank(ypltBz)){
                                ypltBz += "\n";
                            }
                            ypltBz += ypltSize + "、";
                            ypltBz += Optional.ofNullable(xpjl.getFrwlx()).orElseGet(()->"无");
                            ypltBz += " ";
                            ypltBz += xpjl.getFgzkssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzkssj()) : "无";
                            ypltBz += " ";
                            ypltBz += xpjl.getFgzjssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzjssj()) : "无";
                            ypltBz += " ";
                            ypltBz += Optional.ofNullable(xpjl.getFzhpj()).orElseGet(()->"无");

                        }else if (xpjl.getFlx().equals(JcydjbConstant.LB_YLQXSCLT)){
                            ylqxscltSize += 1;

                            if (StringUtils.isNotBlank(ylqxscltBz)){
                                ylqxscltBz += "\n";
                            }
                            ylqxscltBz += ylqxscltSize + "、";
                            ylqxscltBz += Optional.ofNullable(xpjl.getFrwlx()).orElseGet(()->"无");
                            ylqxscltBz += " ";
                            ylqxscltBz += xpjl.getFgzkssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzkssj()) : "无";
                            ylqxscltBz += " ";
                            ylqxscltBz += xpjl.getFgzjssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzjssj()) : "无";
                            ylqxscltBz += " ";
                            ylqxscltBz += Optional.ofNullable(xpjl.getFzhpj()).orElseGet(()->"无");

                        }else if (xpjl.getFlx().equals(JcydjbConstant.LB_YLQXLC)){
                            ylqxlcSize += 1;

                            if (StringUtils.isNotBlank(ylqxlcBz)){
                                ylqxlcBz += "\n";
                            }
                            ylqxlcBz += ylqxlcSize + "、";
                            ylqxlcBz += Optional.ofNullable(xpjl.getFrwlx()).orElseGet(()->"无");
                            ylqxlcBz += " ";
                            ylqxlcBz += xpjl.getFgzkssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzkssj()) : "无";
                            ylqxlcBz += " ";
                            ylqxlcBz += xpjl.getFgzjssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzjssj()) : "无";
                            ylqxlcBz += " ";
                            ylqxlcBz += Optional.ofNullable(xpjl.getFzhpj()).orElseGet(()->"无");

                        }else if (xpjl.getFlx().equals(JcydjbConstant.LB_HZPJCY)){
                            hzpSize += 1;

                            if (StringUtils.isNotBlank(hzpBz)){
                                hzpBz += "\n";
                            }
                            hzpBz += hzpSize + "、";
                            hzpBz += Optional.ofNullable(xpjl.getFrwlx()).orElseGet(()->"无");
                            hzpBz += " ";
                            hzpBz += xpjl.getFgzkssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzkssj()) : "无";
                            hzpBz += " ";
                            hzpBz += xpjl.getFgzjssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzjssj()) : "无";
                            hzpBz += " ";
                            hzpBz += Optional.ofNullable(xpjl.getFzhpj()).orElseGet(()->"无");

                        }
                    }
                }
            }

            if (StringUtils.isNotBlank(ywlcBz)){
                bz += JcydjbConstant.LB_YPLC + ": " + "\n" + ywlcBz;
            }

            if (StringUtils.isNotBlank(ypzcscBz)){
                if (StringUtils.isNotBlank(bz)){
                    bz += "\n";
                }

                bz += JcydjbConstant.LB_YPZCSC + ": " + "\n" + ypzcscBz;
            }

            if (StringUtils.isNotBlank(ypltBz)){
                if (StringUtils.isNotBlank(bz)){
                    bz += "\n";
                }

                bz += JcydjbConstant.LB_YPLT + ": " + "\n" + ypltBz;
            }

            if (StringUtils.isNotBlank(ylqxscltBz)){
                if (StringUtils.isNotBlank(bz)){
                    bz += "\n";
                }

                bz += JcydjbConstant.LB_YLQXSCLT + ": " + "\n" + ylqxscltBz;
            }

            if (StringUtils.isNotBlank(ylqxlcBz)){
                if (StringUtils.isNotBlank(bz)){
                    bz += "\n";
                }

                bz += JcydjbConstant.LB_YLQXLC + ": " + "\n" + ylqxlcBz;
            }

            if (StringUtils.isNotBlank(hzpBz)){
                if (StringUtils.isNotBlank(bz)){
                    bz += "\n";
                }

                bz += JcydjbConstant.LB_HZPJCY + ": " + "\n" + hzpBz;
            }

            if (ywlcSize > 0){
                jccs += JcydjbConstant.LB_YPLC + "("+ywlcSize+")";
            }

            if (ypzcscSize > 0){
                if (StringUtils.isNotBlank(jccs)){
                    jccs += "/";
                }
                jccs += JcydjbConstant.LB_YPZCSC + "("+ypzcscSize+")";
            }


            if (ypltSize > 0){
                if (StringUtils.isNotBlank(jccs)){
                    jccs += "/";
                }
                jccs += JcydjbConstant.LB_YPLT + "("+ypltSize+")";
            }

            if (ylqxscltSize > 0){
                if (StringUtils.isNotBlank(jccs)){
                    jccs += "/";
                }
                jccs += JcydjbConstant.LB_YLQXSCLT + "("+ylqxscltSize+")";
            }

            if (ylqxlcSize > 0){
                if (StringUtils.isNotBlank(jccs)){
                    jccs += "/";
                }
                jccs += JcydjbConstant.LB_YLQXLC + "("+ylqxlcSize+")";
            }

            if (hzpSize > 0){
                if (StringUtils.isNotBlank(jccs)){
                    jccs += "/";
                }
                jccs += JcydjbConstant.LB_HZPJCY + "("+hzpSize+")";
            }

            int count = 0;
            count = ywlcSize + ypzcscSize + ylqxscltSize + ypltSize + ylqxlcSize + hzpSize;
            isAddMdFlag = (count >= yqjccs) ? true : false;

            if (isAddMdFlag) {
                SpcyJcyPrrymd newRymd = new SpcyJcyPrrymd();
                newRymd.setFprjbxxid(prjbxxid);
                newRymd.setId(GetUuIdUtils.ReplaceUuId());
                newRymd.setFjcyid(jcy.getId());
                newRymd.setFuserid(jcy.getFuserid());

                newRymd.setFgzdw(jcy.getFgzdw());
                newRymd.setRealname(jcy.getRealname());
                newRymd.setSex(jcy.getSex());

                newRymd.setFsfzh(jcy.getFsfzh());
                newRymd.setFsxzy(jcy.getFsxzy());
                newRymd.setFxl(jcy.getFxl());

                newRymd.setFjcyxl(xpjcylb);

                newRymd.setFjccs(jccs);
                newRymd.setFbz(bz);

                newRymd.setFsfsdtj(JcyPrdjConstant.PR_RYMD_SFSDTJ_NO);
                newRymd.setCreateTime(new Date());
                newRymd.setUpdateTime(new Date());
                newRymd.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
                newRymd.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
                newRymd.setSysOrgCode(GetCurrentUserUtils.getCurrentUserOrgCode());
                newRymdList.add(newRymd);
            }
        }

        return newRymdList;
    }

    /** 聘任登记提交完成-手动添加人员-身份变更 */
    private void submitBgPrrySfByYesSdtj(SpcyJcyPrrymd rymd, SpcyJcyPrjbxx query){
        if (StringUtils.isBlank(rymd.getFjcyxl())){
            return;
        }
        /** sfsqzh
         * 新增： 添加新的身份
         * 注销变有效
         * 预备检查员变组员
         * 预备检查员变组员-注销变有效
         * 组员变组长
         * 组员变组长-注销变有效
         * */

        List<String> jcylxList = StringPatternUtils.getListByIds(rymd.getFjcyxl());
        for (String xl : jcylxList) {
            String xllx = SpcyConstant.getJcyXllxByJcylb(xl);
            String jcylb = JcydjbConstant.getJcylbByJcyxl(xl);
//            String jcyxl = JcydjbConstant.getJcyxlByJcyLb(xl);
            String jcyxl = JcydjbConstant.getJcyxlByJcyLb2(xl);

            JcyExistLbBo existLbBo = spcyRsHcyService.verifyLbJcyIsExistList(jcylb, jcyxl, rymd.getFuserid());
            if (existLbBo.getSfcz().equals("否")){
                String sfsqzh = JcyPrdjConstant.PR_SDTJRY_SFSQZH_TJ;
                String newJcyid = GetUuIdUtils.ReplaceUuId();
                spcyRsHcyService.initNewPrLbJcy(newJcyid,rymd.getFuserid(), xllx, jcylb, jcyxl, query.getFprsf(), null, query.getFprwh());
                spcyRsJcysfbgjlService.initPrJcysgbgjl(rymd.getFuserid(), newJcyid, jcylb, jcyxl, "", query.getFprsf(), "", query.getId(), query.getFprwh(), false, sfsqzh);
            }else {
                String sfsqzh = "";

                SpcyRsHcy updateJcy = existLbBo.getJcy();
                if (updateJcy != null && StringUtils.isNotBlank(updateJcy.getFssk())){

                    if (query.getFprsf().equals(SpcyConstant.HCY_SSK_ZY)){

                        if (updateJcy.getFssk().equals(SpcyConstant.HCY_SSK_ZY)){
                            if (StringUtils.isNotBlank(updateJcy.getFstate())
                                    && updateJcy.getFstate().equals(SpcyCommonConstant.JCY_STATE_ZX)) {
                                sfsqzh = JcyPrdjConstant.PR_SDTJRY_SFSQZH_ZXBYX;
                                updateJcy.setFstate(SpcyCommonConstant.JCY_STATE_YX);
                                spcyRsHcyService.updateById(updateJcy);
                            }
                        }
                        else if (updateJcy.getFssk().equals(SpcyConstant.HCY_SSK_YB)){
                            sfsqzh = JcyPrdjConstant.PR_SDTJRY_SFSQZH_YBJCYBZY;
                            if (StringUtils.isNotBlank(updateJcy.getFstate())
                                    && updateJcy.getFstate().equals(SpcyCommonConstant.JCY_STATE_ZX)) {
                                sfsqzh = JcyPrdjConstant.PR_SDTJRY_SFSQZH_YBJCYBZY_ZXBYX;
                                updateJcy.setFstate(SpcyCommonConstant.JCY_STATE_YX);
                            }
                            updateJcy.setFssk(SpcyConstant.HCY_SSK_ZY);
                            spcyRsHcyService.updateById(updateJcy);
                        }

                    }else if (query.getFprsf().equals(SpcyConstant.HCY_SSK_ZZ)){

                        if (updateJcy.getFssk().equals(SpcyConstant.HCY_SSK_ZZ)){
                            if (StringUtils.isNotBlank(updateJcy.getFstate())
                                    && updateJcy.getFstate().equals(SpcyCommonConstant.JCY_STATE_ZX)) {
                                sfsqzh = JcyPrdjConstant.PR_SDTJRY_SFSQZH_ZXBYX;
                                updateJcy.setFstate(SpcyCommonConstant.JCY_STATE_YX);
                                spcyRsHcyService.updateById(updateJcy);
                            }
                        }else if (updateJcy.getFssk().equals(SpcyConstant.HCY_SSK_ZY)){
                            sfsqzh = JcyPrdjConstant.PR_SDTJRY_SFSQZH_ZYBZZ;
                            if (StringUtils.isNotBlank(updateJcy.getFstate())
                                    && updateJcy.getFstate().equals(SpcyCommonConstant.JCY_STATE_ZX)) {
                                sfsqzh = JcyPrdjConstant.PR_SDTJRY_SFSQZH_ZYBZZ_ZXBYX;
                                updateJcy.setFstate(SpcyCommonConstant.JCY_STATE_YX);
                            }

                            updateJcy.setFssk(SpcyConstant.HCY_SSK_ZZ);
                            spcyRsHcyService.updateById(updateJcy);
                        }

                    }

                }

                if (StringUtils.isNotBlank(sfsqzh)) {
                    spcyRsJcysfbgjlService.initPrJcysgbgjl(rymd.getFuserid(), updateJcy.getId(), jcylb, jcyxl, query.getFsxjcyzg(), query.getFprsf(), "", query.getId(), query.getFprwh(), false, sfsqzh);
                }
            }
        }

    }

    /** 聘任登记提交完成-非手动添加人员-身份变更 */
    private void submitBgPrryByNoSdtj(SpcyJcyPrrymd rymd, SpcyJcyPrjbxx query){
        String jcylb = JcydjbConstant.getJcylbByJcyxl(rymd.getFjcyxl());
        String jcyxl = JcydjbConstant.getJcyxlByJcyLb(rymd.getFjcyxl());

        /** 生成身份变更记录 */
//        spcyRsJcysfbgjlService.initPrJcysgbgjl(rymd.getFuserid(), rymd.getFjcyid(), jcylb, jcyxl, query.getFsxjcyzg(), query.getFprsf(),"", query.getId(), query.getFprwh(), false);
        spcyRsJcysfbgjlService.initPrJcysgbgjl(rymd.getFuserid(), rymd.getFjcyid(), jcylb, jcyxl, SpcyConstant.HCY_SSK_YB, query.getFprsf(),"", query.getId(), query.getFprwh(), false);

        LambdaUpdateWrapper<SpcyRsHcy> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcyRsHcy::getId, rymd.getFjcyid());

        SpcyRsHcy updateJcy = new SpcyRsHcy();
        updateJcy.setFssk(query.getFprsf());
        spcyRsHcyService.update(updateJcy, updateWrapper);
    }

    /** 聘任登记提交完成-非手动添加人员-身份变更 (版本2) */
    private void submitBgPrryByNoSdtj2(SpcyJcyPrrymd rymd, SpcyJcyPrjbxx query){
        if (StringUtils.isBlank(rymd.getFjcyxl())){
            return;
        }

        /** sfsqzh
         * 新增： 添加新的身份
         * 注销变有效
         * 预备检查员变组员
         * 预备检查员变组员-注销变有效
         * 组员变组长
         * 组员变组长-注销变有效
         * */

        List<String> jcylxList = StringPatternUtils.getListByIds(rymd.getFjcyxl());
        for (String xl : jcylxList) {
            String xllx = SpcyConstant.getJcyXllxByJcylb(xl);
            String jcylb = JcydjbConstant.getJcylbByJcyxl(xl);
            String jcyxl = JcydjbConstant.getJcyxlByJcyLb2(xl);

            JcyExistLbBo existLbBo = spcyRsHcyService.verifyLbJcyIsExistList(jcylb, jcyxl, rymd.getFuserid());
            if (existLbBo.getSfcz().equals("否")){
                String sfsqzh = JcyPrdjConstant.PR_SDTJRY_SFSQZH_TJ;
                String newJcyid = GetUuIdUtils.ReplaceUuId();
                spcyRsHcyService.initNewPrLbJcy(newJcyid,rymd.getFuserid(), xllx, jcylb, jcyxl, query.getFprsf(), null, query.getFprwh());
                spcyRsJcysfbgjlService.initPrJcysgbgjl(rymd.getFuserid(), newJcyid, jcylb, jcyxl, "", query.getFprsf(), "", query.getId(), query.getFprwh(), false, sfsqzh);
            }else {
                String sfsqzh = "";

                SpcyRsHcy updateJcy = existLbBo.getJcy();
                if (updateJcy != null && StringUtils.isNotBlank(updateJcy.getFssk())){

                    if (query.getFprsf().equals(SpcyConstant.HCY_SSK_ZY)){

                        if (updateJcy.getFssk().equals(SpcyConstant.HCY_SSK_ZY)){
                            if (StringUtils.isNotBlank(updateJcy.getFstate())
                                    && updateJcy.getFstate().equals(SpcyCommonConstant.JCY_STATE_ZX)) {
                                sfsqzh = JcyPrdjConstant.PR_SDTJRY_SFSQZH_ZXBYX;
                                updateJcy.setFstate(SpcyCommonConstant.JCY_STATE_YX);
                                spcyRsHcyService.updateById(updateJcy);
                            }
                        }
                        else if (updateJcy.getFssk().equals(SpcyConstant.HCY_SSK_YB)){
                            sfsqzh = JcyPrdjConstant.PR_SDTJRY_SFSQZH_YBJCYBZY;
                            if (StringUtils.isNotBlank(updateJcy.getFstate())
                                    && updateJcy.getFstate().equals(SpcyCommonConstant.JCY_STATE_ZX)) {
                                sfsqzh = JcyPrdjConstant.PR_SDTJRY_SFSQZH_YBJCYBZY_ZXBYX;
                                updateJcy.setFstate(SpcyCommonConstant.JCY_STATE_YX);
                            }
                            updateJcy.setFssk(SpcyConstant.HCY_SSK_ZY);
                            spcyRsHcyService.updateById(updateJcy);
                        }

                    }else if (query.getFprsf().equals(SpcyConstant.HCY_SSK_ZZ)){

                        if (updateJcy.getFssk().equals(SpcyConstant.HCY_SSK_ZZ)){
                            if (StringUtils.isNotBlank(updateJcy.getFstate())
                                    && updateJcy.getFstate().equals(SpcyCommonConstant.JCY_STATE_ZX)) {
                                sfsqzh = JcyPrdjConstant.PR_SDTJRY_SFSQZH_ZXBYX;
                                updateJcy.setFstate(SpcyCommonConstant.JCY_STATE_YX);
                                spcyRsHcyService.updateById(updateJcy);
                            }
                        }else if (updateJcy.getFssk().equals(SpcyConstant.HCY_SSK_ZY)){
                            sfsqzh = JcyPrdjConstant.PR_SDTJRY_SFSQZH_ZYBZZ;
                            if (StringUtils.isNotBlank(updateJcy.getFstate())
                                    && updateJcy.getFstate().equals(SpcyCommonConstant.JCY_STATE_ZX)) {
                                sfsqzh = JcyPrdjConstant.PR_SDTJRY_SFSQZH_ZYBZZ_ZXBYX;
                                updateJcy.setFstate(SpcyCommonConstant.JCY_STATE_YX);
                            }

                            updateJcy.setFssk(SpcyConstant.HCY_SSK_ZZ);
                            spcyRsHcyService.updateById(updateJcy);
                        }

                    }

                }

                if (StringUtils.isNotBlank(sfsqzh)) {
                    spcyRsJcysfbgjlService.initPrJcysgbgjl(rymd.getFuserid(), updateJcy.getId(), jcylb, jcyxl, query.getFsxjcyzg(), query.getFprsf(), "", query.getId(), query.getFprwh(), false, sfsqzh);
                }
            }
        }
    }

    /** 聘任登记退回完成-手动添加人员-身份变更 */
    private void returnBgPrryByYesSdtj(SpcyJcyPrrymd rymd, SpcyJcyPrjbxx query){
        if (StringUtils.isBlank(rymd.getFjcyxl())){
            return;
        }

        List<SpcyRsJcysfbgjl> bgjlList = spcyRsJcysfbgjlService.list(new LambdaQueryWrapper<SpcyRsJcysfbgjl>()
                .eq(SpcyRsJcysfbgjl::getFpid, query.getId())
                .isNotNull(SpcyRsJcysfbgjl::getFsfsqzh));

        if (CollectionUtil.isEmpty(bgjlList)){
            return;
        }

        for (SpcyRsJcysfbgjl bgjl : bgjlList){
            boolean flag = false;
            LambdaUpdateWrapper<SpcyRsHcy> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcyRsHcy::getId, bgjl.getFjcyid());

            SpcyRsHcy updateJcy = new SpcyRsHcy();
            if (bgjl.getFsfsqzh().equals(JcyPrdjConstant.PR_SDTJRY_SFSQZH_TJ)){
                spcyRsHcyService.removeById(bgjl.getFjcyid());

            }else if (bgjl.getFsfsqzh().equals(JcyPrdjConstant.PR_SDTJRY_SFSQZH_ZXBYX)){
                flag = true;
                updateJcy.setFstate(SpcyCommonConstant.JCY_STATE_ZX);

            }else if (bgjl.getFsfsqzh().equals(JcyPrdjConstant.PR_SDTJRY_SFSQZH_YBJCYBZY)){
                flag = true;
                updateJcy.setFssk(SpcyConstant.HCY_SSK_YB);

            }else if (bgjl.getFsfsqzh().equals(JcyPrdjConstant.PR_SDTJRY_SFSQZH_YBJCYBZY_ZXBYX)){
                flag = true;
                updateJcy.setFstate(SpcyCommonConstant.JCY_STATE_ZX);
                updateJcy.setFssk(SpcyConstant.HCY_SSK_YB);

            }else if (bgjl.getFsfsqzh().equals(JcyPrdjConstant.PR_SDTJRY_SFSQZH_ZYBZZ)){
                flag = true;
                updateJcy.setFssk(SpcyConstant.HCY_SSK_ZY);

            }else if (bgjl.getFsfsqzh().equals(JcyPrdjConstant.PR_SDTJRY_SFSQZH_ZYBZZ_ZXBYX)){
                flag = true;
                updateJcy.setFstate(SpcyCommonConstant.JCY_STATE_ZX);
                updateJcy.setFssk(SpcyConstant.HCY_SSK_ZY);

            }

            if (flag) {
                spcyRsHcyService.update(updateJcy, updateWrapper);
            }

            spcyRsJcysfbgjlService.removeById(bgjl.getId());
        }

        /** 生成身份变更记录 */
//        spcyRsJcysfbgjlService.initPrJcysgbgjl(rymd.getFuserid(), rymd.getFjcyid(), "", "", query.getFsxjcyzg(), query.getFprsf(), "", query.getId(), query.getFprwh(), true);
    }

    /** 聘任登记退回完成-非手动添加人员-身份变更 */
    private void returnBgPrryByNoSdtj(SpcyJcyPrrymd rymd, SpcyJcyPrjbxx query){
        String jcylb = JcydjbConstant.getJcylbByJcyxl(rymd.getFjcyxl());
        String jcyxl = JcydjbConstant.getJcyxlByJcyLb(rymd.getFjcyxl());

        /** 生成身份变更记录 */
//        spcyRsJcysfbgjlService.initPrJcysgbgjl(rymd.getFuserid(), rymd.getFjcyid(), jcylb, jcyxl, query.getFsxjcyzg(), query.getFprsf(), "", query.getId(), query.getFprwh(), true);

        LambdaUpdateWrapper<SpcyRsHcy> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcyRsHcy::getId, rymd.getFjcyid());

        SpcyRsHcy updateJcy = new SpcyRsHcy();
//        updateJcy.setFssk(query.getFsxjcyzg());
        updateJcy.setFssk(SpcyConstant.HCY_SSK_YB);
        spcyRsHcyService.update(updateJcy, updateWrapper);

        /** 生成身份变更记录 */
        List<SpcyRsJcysfbgjl> bgjlList = spcyRsJcysfbgjlService.list(new LambdaQueryWrapper<SpcyRsJcysfbgjl>()
                .eq(SpcyRsJcysfbgjl::getFpid, query.getId())
                .isNull(SpcyRsJcysfbgjl::getFsfsqzh));

        if (CollectionUtil.isEmpty(bgjlList)){
            return;
        }

        for (SpcyRsJcysfbgjl bgjl : bgjlList){
            spcyRsJcysfbgjlService.removeById(bgjl.getId());
        }
    }

    /** 聘任登记退回完成-非手动添加人员-身份变更 (版本2) */
    private void returnBgPrryByNoSdtj2(SpcyJcyPrrymd rymd, SpcyJcyPrjbxx query){
        if (StringUtils.isBlank(rymd.getFjcyxl())){
            return;
        }

        List<SpcyRsJcysfbgjl> bgjlList = spcyRsJcysfbgjlService.list(new LambdaQueryWrapper<SpcyRsJcysfbgjl>()
                .eq(SpcyRsJcysfbgjl::getFpid, query.getId())
                .isNotNull(SpcyRsJcysfbgjl::getFsfsqzh));

        if (CollectionUtil.isEmpty(bgjlList)){
            return;
        }

        for (SpcyRsJcysfbgjl bgjl : bgjlList){
            boolean flag = false;
            LambdaUpdateWrapper<SpcyRsHcy> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcyRsHcy::getId, bgjl.getFjcyid());

            SpcyRsHcy updateJcy = new SpcyRsHcy();
            if (bgjl.getFsfsqzh().equals(JcyPrdjConstant.PR_SDTJRY_SFSQZH_TJ)){
                spcyRsHcyService.removeById(bgjl.getFjcyid());

            }else if (bgjl.getFsfsqzh().equals(JcyPrdjConstant.PR_SDTJRY_SFSQZH_ZXBYX)){
                flag = true;
                updateJcy.setFstate(SpcyCommonConstant.JCY_STATE_ZX);

            }else if (bgjl.getFsfsqzh().equals(JcyPrdjConstant.PR_SDTJRY_SFSQZH_YBJCYBZY)){
                flag = true;
                updateJcy.setFssk(SpcyConstant.HCY_SSK_YB);

            }else if (bgjl.getFsfsqzh().equals(JcyPrdjConstant.PR_SDTJRY_SFSQZH_YBJCYBZY_ZXBYX)){
                flag = true;
                updateJcy.setFstate(SpcyCommonConstant.JCY_STATE_ZX);
                updateJcy.setFssk(SpcyConstant.HCY_SSK_YB);

            }else if (bgjl.getFsfsqzh().equals(JcyPrdjConstant.PR_SDTJRY_SFSQZH_ZYBZZ)){
                flag = true;
                updateJcy.setFssk(SpcyConstant.HCY_SSK_ZY);

            }else if (bgjl.getFsfsqzh().equals(JcyPrdjConstant.PR_SDTJRY_SFSQZH_ZYBZZ_ZXBYX)){
                flag = true;
                updateJcy.setFstate(SpcyCommonConstant.JCY_STATE_ZX);
                updateJcy.setFssk(SpcyConstant.HCY_SSK_ZY);

            }

            if (flag) {
                spcyRsHcyService.update(updateJcy, updateWrapper);
            }

            spcyRsJcysfbgjlService.removeById(bgjl.getId());
        }
    }

    /** 聘任登记-手动添加人员名单 */
    private SpcyJcyPrrymd sddjRymdService(SpcyJcyPrjbxx prdj, YcSysUser user, String prjbxxid){
        // 是否已经存在
        List<SpcyJcyPrrymd> rymdUserList = spcyJcyPrrymdService.list(new LambdaQueryWrapper<SpcyJcyPrrymd>()
                .eq(SpcyJcyPrrymd::getFprjbxxid, prjbxxid)
                .eq(SpcyJcyPrrymd::getFuserid, user.getId()));
        if (CollectionUtil.isNotEmpty(rymdUserList)){
            return null;
        }

        String bz = "";
        LambdaQueryWrapper<SpcySqsxXpjl> xpQueryWrapper = new LambdaQueryWrapper<>();
        xpQueryWrapper.eq(SpcySqsxXpjl::getFuserid, user.getId());
        xpQueryWrapper.eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_YES);
        if (StringEmptyUtils.isNotBlank(prdj.getFxpjcylb())){
            xpQueryWrapper.in(SpcySqsxXpjl::getFlx, StringPatternUtils.getListByIds(prdj.getFxpjcylb()));
        }
        if (StringEmptyUtils.isNotBlank(prdj.getFyqkpjg())){
            xpQueryWrapper.in(SpcySqsxXpjl::getFzhpj, StringPatternUtils.getListByIds(prdj.getFyqkpjg()));
        }
        if (StringEmptyUtils.isNotBlank(prdj.getFsxkssj())){
            xpQueryWrapper.ge(SpcySqsxXpjl::getFgzkssj, DateConversionUtils.convertStringToDateYmd(prdj.getFsxkssj()));
        }
        if (StringEmptyUtils.isNotBlank(prdj.getFsxjssj())){
            xpQueryWrapper.le(SpcySqsxXpjl::getFgzjssj, DateConversionUtils.convertStringToDateYmd(prdj.getFsxjssj()));
        }
        List<SpcySqsxXpjl> xpjlList = spcySqsxXpjlService.list(xpQueryWrapper);


        int ywlcSize = 0;
        String ywlcBz = "";

        int ypzcscSize = 0;
        String ypzcscBz = "";

        int ypltSize = 0;
        String ypltBz = "";

        int ylqxscltSize = 0;
        String ylqxscltBz = "";

        int ylqxlcSize = 0;
        String ylqxlcBz = "";

        int hzpSize = 0;
        String hzpBz = "";

        if (CollectionUtil.isNotEmpty(xpjlList)){
            for (SpcySqsxXpjl xpjl : xpjlList){
                if (StringUtils.isNotBlank(xpjl.getFlx())){
                    if (xpjl.getFlx().equals(JcydjbConstant.LB_YPLC)){
                        ywlcSize += 1;

                        if (StringUtils.isNotBlank(ywlcBz)){
                            ywlcBz += "\n";
                        }
                        ywlcBz += ywlcSize + "、";
                        ywlcBz += Optional.ofNullable(xpjl.getFrwlx()).orElseGet(()->"无");
                        ywlcBz += " ";
                        ywlcBz += xpjl.getFgzkssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzkssj()) : "无";
                        ywlcBz += " ";
                        ywlcBz += xpjl.getFgzjssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzjssj()) : "无";
                        ywlcBz += " ";
                        ywlcBz += Optional.ofNullable(xpjl.getFzhpj()).orElseGet(()->"无");

                    }else if (xpjl.getFlx().equals(JcydjbConstant.LB_YPZCSC)){
                        ypzcscSize += 1;

                        if (StringUtils.isNotBlank(ypzcscBz)){
                            ypzcscBz += "\n";
                        }
                        ypzcscBz += ypzcscSize + "、";
                        ypzcscBz += Optional.ofNullable(xpjl.getFrwlx()).orElseGet(()->"无");
                        ypzcscBz += " ";
                        ypzcscBz += xpjl.getFgzkssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzkssj()) : "无";
                        ypzcscBz += " ";
                        ypzcscBz += xpjl.getFgzjssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzjssj()) : "无";
                        ypzcscBz += " ";
                        ypzcscBz += Optional.ofNullable(xpjl.getFzhpj()).orElseGet(()->"无");

                    }else if (xpjl.getFlx().equals(JcydjbConstant.LB_YPLT)){
                        ypltSize += 1;

                        if (StringUtils.isNotBlank(ypltBz)){
                            ypltBz += "\n";
                        }
                        ypltBz += ypltSize + "、";
                        ypltBz += Optional.ofNullable(xpjl.getFrwlx()).orElseGet(()->"无");
                        ypltBz += " ";
                        ypltBz += xpjl.getFgzkssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzkssj()) : "无";
                        ypltBz += " ";
                        ypltBz += xpjl.getFgzjssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzjssj()) : "无";
                        ypltBz += " ";
                        ypltBz += Optional.ofNullable(xpjl.getFzhpj()).orElseGet(()->"无");

                    }else if (xpjl.getFlx().equals(JcydjbConstant.LB_YLQXSCLT)){
                        ylqxscltSize += 1;

                        if (StringUtils.isNotBlank(ylqxscltBz)){
                            ylqxscltBz += "\n";
                        }
                        ylqxscltBz += ylqxscltSize + "、";
                        ylqxscltBz += Optional.ofNullable(xpjl.getFrwlx()).orElseGet(()->"无");
                        ylqxscltBz += " ";
                        ylqxscltBz += xpjl.getFgzkssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzkssj()) : "无";
                        ylqxscltBz += " ";
                        ylqxscltBz += xpjl.getFgzjssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzjssj()) : "无";
                        ylqxscltBz += " ";
                        ylqxscltBz += Optional.ofNullable(xpjl.getFzhpj()).orElseGet(()->"无");

                    }else if (xpjl.getFlx().equals(JcydjbConstant.LB_YLQXLC)){
                        ylqxlcSize += 1;

                        if (StringUtils.isNotBlank(ylqxlcBz)){
                            ylqxlcBz += "\n";
                        }
                        ylqxlcBz += ylqxlcSize + "、";
                        ylqxlcBz += Optional.ofNullable(xpjl.getFrwlx()).orElseGet(()->"无");
                        ylqxlcBz += " ";
                        ylqxlcBz += xpjl.getFgzkssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzkssj()) : "无";
                        ylqxlcBz += " ";
                        ylqxlcBz += xpjl.getFgzjssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzjssj()) : "无";
                        ylqxlcBz += " ";
                        ylqxlcBz += Optional.ofNullable(xpjl.getFzhpj()).orElseGet(()->"无");

                    }else if (xpjl.getFlx().equals(JcydjbConstant.LB_HZPJCY)){
                        hzpSize += 1;

                        if (StringUtils.isNotBlank(hzpBz)){
                            hzpBz += "\n";
                        }
                        hzpBz += hzpSize + "、";
                        hzpBz += Optional.ofNullable(xpjl.getFrwlx()).orElseGet(()->"无");
                        hzpBz += " ";
                        hzpBz += xpjl.getFgzkssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzkssj()) : "无";
                        hzpBz += " ";
                        hzpBz += xpjl.getFgzjssj() != null ? DateConversionUtils.DateToStringYYYYMMDD(xpjl.getFgzjssj()) : "无";
                        hzpBz += " ";
                        hzpBz += Optional.ofNullable(xpjl.getFzhpj()).orElseGet(()->"无");

                    }
                }
            }
        }


        if (StringUtils.isNotBlank(ywlcBz)){
            bz += JcydjbConstant.LB_YPLC + ": " + "\n" + ywlcBz;
        }

        if (StringUtils.isNotBlank(ypzcscBz)){
            if (StringUtils.isNotBlank(bz)){
                bz += "\n";
            }

            bz += JcydjbConstant.LB_YPZCSC + ": " + "\n" + ypzcscBz;
        }

        if (StringUtils.isNotBlank(ypltBz)){
            if (StringUtils.isNotBlank(bz)){
                bz += "\n";
            }

            bz += JcydjbConstant.LB_YPLT + ": " + "\n" + ypltBz;
        }

        if (StringUtils.isNotBlank(ylqxscltBz)){
            if (StringUtils.isNotBlank(bz)){
                bz += "\n";
            }

            bz += JcydjbConstant.LB_YLQXSCLT + ": " + "\n" + ylqxscltBz;
        }

        if (StringUtils.isNotBlank(ylqxlcBz)){
            if (StringUtils.isNotBlank(bz)){
                bz += "\n";
            }

            bz += JcydjbConstant.LB_YLQXLC + ": " + "\n" + ylqxlcBz;
        }

        if (StringUtils.isNotBlank(hzpBz)){
            if (StringUtils.isNotBlank(bz)){
                bz += "\n";
            }

            bz += JcydjbConstant.LB_HZPJCY + ": " + "\n" + hzpBz;
        }

        SpcyJcyPrrymd newRymd = new SpcyJcyPrrymd();
        newRymd.setFprjbxxid(prdj.getId());
        newRymd.setId(GetUuIdUtils.ReplaceUuId());

        newRymd.setFgzdw(user.getFgzdw());
        newRymd.setRealname(user.getRealname());
        newRymd.setSex(user.getSex());

        newRymd.setFsfzh(user.getFsfzh());
        newRymd.setFsxzy(user.getFsxzy());
        newRymd.setFxl(user.getFxl());
        newRymd.setFuserid(user.getId());

        newRymd.setFjcyxl(prdj.getFxpjcylb());

        String jccs = "";

        if (ywlcSize > 0){
            jccs += JcydjbConstant.LB_YPLC + "("+ywlcSize+")";
        }

        if (ypzcscSize > 0){
            if (StringUtils.isNotBlank(jccs)){
                jccs += "/";
            }
            jccs += JcydjbConstant.LB_YPZCSC + "("+ypzcscSize+")";
        }


        if (ypltSize > 0){
            if (StringUtils.isNotBlank(jccs)){
                jccs += "/";
            }
            jccs += JcydjbConstant.LB_YPLT + "("+ypltSize+")";
        }

        if (ylqxscltSize > 0){
            if (StringUtils.isNotBlank(jccs)){
                jccs += "/";
            }
            jccs += JcydjbConstant.LB_YLQXSCLT + "("+ylqxscltSize+")";
        }

        if (ylqxlcSize > 0){
            if (StringUtils.isNotBlank(jccs)){
                jccs += "/";
            }
            jccs += JcydjbConstant.LB_YLQXLC + "("+ylqxlcSize+")";
        }

        if (hzpSize > 0){
            if (StringUtils.isNotBlank(jccs)){
                jccs += "/";
            }
            jccs += JcydjbConstant.LB_HZPJCY + "("+hzpSize+")";
        }

        newRymd.setFjccs(jccs);
        newRymd.setFbz(bz);

        newRymd.setFsfsdtj(JcyPrdjConstant.PR_RYMD_SFSDTJ_YES);
        newRymd.setCreateTime(new Date());
        newRymd.setUpdateTime(new Date());
        newRymd.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
        newRymd.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        newRymd.setSysOrgCode(GetCurrentUserUtils.getCurrentUserOrgCode());

        return newRymd;
    }
}
